﻿#region 命名空间

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("Bank")]
    public partial class Bank : WebFactory.PageBase
    {
        private readonly BankManage _myBll = new BankManage();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var Action = Request["Action"];
                if (Action == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = false;
                }
                else if (Action == "Edit")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = true;
                    this.btnOK.Visible = false;


                    var model = new BankManage().GetInfo(Request["ID"]);
                    this.txtBankName.Text = model.BankName;
                    this.txtBankFullName.Text = model.BankName;
                    this.txtHolderName.Text = model.HolderName;
                    this.txtCardNum.Text = model.CardNum;
                    this.txtAccounts.Text = model.Accounts;
                    this.txtIntro.Text = model.Intro;
                    this.cbkPass.Checked = model.Pass;
                    this.cbkIsDefault.Checked = model.IsDefault;
                }
                else
                {
                    BindList();
                }
            }
        }

        private void BindList()
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);
            this.GV_List.DataSource = new BankManage().GetList();
            this.GV_List.DataBind();

            if (this.GV_List.Rows.Count <= 0)
            {
                this.DelSeletedID.Enabled = false;
            }
        }

        protected string ZhangHao(object A, object B)
        {
            if (A.ToString().Length == 0)
            {
                return B.ToString();
            }
            return A.ToString();
        }


        protected void DelSeletedID_Click(object sender, EventArgs e)
        {
            this._myBll.Delete(GetSelectedRow.GetIDs(this.GV_List), this.UpdatePanel1);
            BindList();
        }

        #region 添加或修改

        private void Add()
        {
            var model = new WinnerCMS.Model.Bank();

            model.BankName = this.txtBankName.Text.Trim();
            model.BankFullName = this.txtBankFullName.Text.Trim();
            model.HolderName = this.txtHolderName.Text.Trim();
            model.Accounts = this.txtAccounts.Text.Trim();
            model.CardNum = this.txtCardNum.Text.Trim();
            model.Intro = this.txtIntro.Text.Trim();
            model.IsDefault = this.cbkIsDefault.Checked;
            model.Pass = this.cbkPass.Checked;
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else
            {
                model.Id = int.Parse(Request["ID"]);
            }

            var resu = new BankManage().Add(model);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
            else
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList();
            }
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Bank.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                this._myBll.PassOrNo(this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());

                BindList();
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBll.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString(), this.UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[5];
                var lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
    }
}